﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SlamDunk.CMS.Model
{
    /// <summary>
    /// Channel实体类（手写部分）
    /// </summary>
    public partial class Channel
    {
        #region 01.方法——转换为视图模型对象
        /// <summary>
        /// 转换为视图模型对象方法
        /// </summary>
        /// <returns>视图模型对象</returns>
        public Model.ChannelView ToViewModel()
        {
            return new Model.ChannelView()
            {
                Id = this.Id,
                PId = this.PId,
                ChannelName = this.ChannelName,
                Abbr = this.Abbr,
                Description = this.Description,
                IndexTemplate = this.IndexTemplate,
                ListTemplate = this.ListTemplate,
                DetailTemplate = this.DetailTemplate,
                PageSize = this.PageSize,
                Sort = this.Sort,
                IsLink = this.IsLink,
                IsNavigator = this.IsNavigator,
                Url = this.GetUrl()
            };
        }
        #endregion

        #region 02.方法——栏目实体转换为树形节点
        /// <summary>
        /// 菜单实体转换为树形节点方法
        /// </summary>
        /// <returns>树形节点</returns>
        public Model.TreeModel ToTreeNode()
        {
            //返回TreeModel
            return new Model.TreeModel()
            {
                id = this.Id,
                text = this.ChannelName,
                state = "open",
                attributes = new { href = string.Format("/Admin/Article/Index/{0}", this.Id), isLink = this.IsLink == true ? 1 : 0 },
                @checked = false
            };
        }
        #endregion

        #region 03.方法——获取栏目Url地址
        /// <summary>
        /// 方法——获取栏目Url地址
        /// </summary>
        /// <returns>栏目Url地址</returns>
        public string GetUrl()
        {
            if (this.IsLink == true)
            {
                string indexPage = string.Format("{0}/Index.html", this.SaveDirectory);
                string listPage = string.Format("{0}/List-1.html", this.SaveDirectory);
                if (!string.IsNullOrEmpty(this.IndexTemplate))
                {
                    return indexPage;
                }
                else if (!string.IsNullOrEmpty(this.ListTemplate))
                {
                    return listPage;
                }
            }
            return string.Empty;
        }
        #endregion
    }
}
